package com.rsstuan.storage.impl;


import javax.jdo.PersistenceManager;

import com.rsstuan.data.Channel;
import com.rsstuan.storage.IChannelStorage;



public class GoogleChannelStorage implements IChannelStorage {
	 
	

	@Override
	public void saveChannel(Channel channel) {
		PersistenceManager pm = PMF.get().getPersistenceManager(); 
		try {
			pm.makePersistent(channel);
		}
		finally {
			pm.close();
		}
		
	}
	@Override
	public Channel getChannel(Long channelId) {
		PersistenceManager pm = PMF.get().getPersistenceManager(); 
		String query = "select from " + Channel.class.getName() + " where id ="
				+ channelId;
		Channel channel = (Channel) pm.newQuery(query).execute();
		return channel;

	}
	
	@Override
	public void updateArticleCount(Channel channel, int newArticle) {
		PersistenceManager pm = PMF.get().getPersistenceManager(); 
		String query = "update " + Channel.class.getName()
				+ " set articleCount = articleCount + " + newArticle
				+ " where id =" + channel.getId();
		pm.newQuery(query).execute();
	
	}
	@Override
	public void updateChannelInfo(Channel channel) {
		PersistenceManager pm = PMF.get().getPersistenceManager(); 
		String query = "update " + Channel.class.getName()
				+ " set title = " + channel.getTitle()
				+ " description = " + channel.getDescription()
				+ " author = " + channel.getAuthor()
				+ " link = " + channel.getLink()
				+ " where channelId = " + channel.getId();
		
		pm.newQuery(query).execute();
	}
	@Override
	public void updateSuccess(Channel channel) {
		PersistenceManager pm = PMF.get().getPersistenceManager(); 
		String query = "update " + Channel.class.getName()
				+ " set lastUpdateTime = " + channel.getLastUpdateTime()
				+ " where channelId = " + channel.getId();

		pm.newQuery(query).execute();

	}
	

	  
	  


}
